package cn.android52.filepicker.ui.adapter

import android.annotation.SuppressLint
import android.view.View
import androidx.recyclerview.widget.RecyclerView

abstract class CommonAdapter<E : Any, VH : RecyclerView.ViewHolder>(private val mData: MutableList<E> = mutableListOf()) : RecyclerView.Adapter<VH>() {

    private var mListener: OnItemClickListener<E, VH>? = null

    override fun getItemCount(): Int = mData.size

    fun getOriginData(): MutableList<E> = mData

    fun getData(): List<E> = mData.toList()

    fun getItem(position: Int): E? = mData.getOrNull(position)

    @SuppressLint("NotifyDataSetChanged")
    fun resetData(data: MutableList<E>) {
        mData.clear()
        mData.addAll(data)
        notifyDataSetChanged()
    }

    fun setOnItemClickListener(listener: OnItemClickListener<E, VH>) {
        mListener = listener
    }

    protected fun onClick(view: View, item: E?, holder: VH, position: Int) {
        mListener?.onItemClick(view, item, holder, position)
    }

    fun interface OnItemClickListener<E, VH> {

        fun onItemClick(view: View, item: E?, holder: VH, position: Int)
    }
}